package com.jc.oss.adapter;

import com.jc.oss.entity.fileInfo;
import com.jc.oss.adapter.StorageAdapter;
import com.jc.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @Author gongjiawei
 * @Date 2024/9/4 17:06
 * @注释
 */

public class MinioStorageAdapter implements StorageAdapter {
    @Resource
    private MinioUtil minioUtil;
    /**
     * minioUrl
     */
    @Value("${minio.url}")
    private String url;

    @SneakyThrows
    @Override
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @SneakyThrows
    @Override
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {

        minioUtil.createBucket(bucket);
        if (objectName!=null){
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket,objectName+"/"+uploadFile.getName());

        }
        else {
            minioUtil.uploadFile(uploadFile.getInputStream(),bucket,uploadFile.getName());

        }
    }
    @SneakyThrows
    @Override
    public List<String> getAllBucket() {
        return minioUtil.getAllBucket();
    }
    @SneakyThrows
    @Override
    public List<fileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }
    @SneakyThrows
    @Override
    public InputStream download(String bucket, String objectName) {
        return minioUtil.download(bucket,objectName);
    }
    @SneakyThrows
    @Override
    public void delete(String bucket) {
        minioUtil.delete(bucket);

    }
    @SneakyThrows
    @Override
    public void delete(String bucket, String objectName) {
        minioUtil.delete(bucket,objectName);

    }


    @Override
    @SneakyThrows
    public String getUrl(String bucket, String objectName) {
        return url + "/" + bucket + "/" + objectName;
    }

}
